#include "evaui.h"
#include "ui_evaui.h"

evaui::evaui(QSqlDatabase* db,QString account,QString student,QStringList tname,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::evaui)
{
    ui->setupUi(this);

    this->setFixedSize(this->size());

    this->tname = tname;

    ui->combTeacher->addItems(this->tname);

    m_account = account;
    m_student = student;
    m_db = *db;
}

evaui::~evaui()
{
    delete ui;
}

void evaui::on_btnAccept_clicked()
{
    QSqlQuery query;
    query.exec("select * from tb_teacher where name='"+ui->combTeacher->currentText()+"';");
    query.first();
    if(!query.isValid()){
        return;
    }

    QString teacherId = query.value("id").toString();

    query.exec("select * from tb_evaluate;");
    for(query.first();query.isValid();query.next()){
        if(query.value("studentid").toString() == m_account && query.value("teacherid").toString() == teacherId){
            QMessageBox::warning(0,"警告","已评价过该老师!");
            return;
        }
    }

    if(query.exec("insert into tb_evaluate(studentid,studentname,teacherid,teachername,evalute,score)"
               " values("+m_account+",'"+m_student+"',"+teacherId+",'"+ui->combTeacher->currentText()+"','"+ui->editEva->toPlainText()+"',"+ui->spinScore->text()+");")){
        QMessageBox::information(0,"提示","提交成功!");
//        m_evaModel->select();

        //筛选数据
//        for(int i = 0;i < m_evaModel->rowCount(); ++i){
//            ui->tvEva->showRow(i);
//            if(m_evaModel->data(m_evaModel->index(i,1)).toString() != m_account){
//                ui->tvEva->hideRow(i);
//            }
//        }
        this->accept();
    }
}


void evaui::on_btnCancel_clicked()
{
    this->reject();
}

